package com.acce.gxsc.page.supervision.carInMarket.child;

import com.acce.gxsc.model.WorkModel;
import com.acce.gxsc.model.loaclBean.ResultBean;
import com.acce.gxsc.model.netBean.InCarListBean;
import com.acce.sdk.base.mvp.BasePresenter;
import com.acce.sdk.utils.ToastUtils;

import rx.Subscriber;

/**
 * Created by huxley on 2017/5/26.
 */

public class CarInMarketChildPresenter extends BasePresenter<CarInMarketChildView> {
    String  time;
    boolean isLoadOver;
    String  direction = "";
    public void loadCarInMarketList(String queryTime, String queryType, final boolean isLoadMore) {


            if (mViewRef != null && isLoadMore && isLoadOver) {
                getView().setRefreshing(false);
                return;
            }
            if (!isLoadMore) {
                isLoadOver = false;
                direction = "1";
                time = "";
            } else {
                direction = "0";
            }
            WorkModel.getInstance().loadCarInMarketList(queryTime, queryType, time, direction).subscribe(new Subscriber<ResultBean<InCarListBean>>() {
                @Override
                public void onStart() {
                    if (mViewRef != null) {
                        getView().setRefreshing(true);
                    }
                }

                @Override
                public void onNext(ResultBean<InCarListBean> datas) {
                    if (mViewRef != null) {
                        if (!"200".equals(datas.returnCode)) {
                            ToastUtils.show(datas.returnMsg);
                            return;
                        }
                        if (datas.result.inCarList == null || datas.result.inCarList.size() <= 0) {
                            isLoadOver = true;
                            ToastUtils.show("没有更多");
                            if (isLoadMore) {
                                return;
                            }
                        } else {
                            time = datas.result.inCarList.get(datas.result.inCarList.size() - 1).createTime;
                        }
                        getView().loadCarInMarketListSuccess(isLoadMore, datas.result.inCarList);
                    }
                }

                @Override
                public void onCompleted() {
                    if (mViewRef != null) {
                        getView().setRefreshing(false);
                    }
                }

                @Override
                public void onError(Throwable e) {
                    if (mViewRef != null) {
                        e.printStackTrace();
                        getView().setRefreshing(false);
                    }
                }
            });
    }

    public void loadCarInMarketCount(String queryTime, String queryType) {
        WorkModel.getInstance().loadCarInMarketCount(queryTime, queryType).subscribe(new Subscriber<ResultBean<InCarListBean>>() {
            @Override
            public void onStart() {
                if (mViewRef != null) {
                    getView().setRefreshing(true);
                }
            }

            @Override
            public void onNext(ResultBean<InCarListBean> datas) {
                if (mViewRef != null) {
                    getView().loadCarInMarketCount(datas.result.cartotal.totalCar);
                }
            }

            @Override
            public void onCompleted() {
                if (mViewRef != null) {
                    getView().setRefreshing(false);
                }
            }

            @Override
            public void onError(Throwable e) {
                if (mViewRef != null) {
                    e.printStackTrace();
                    getView().setRefreshing(false);
                }
            }
        });
    }
}
